import { isObject } from "@/utils"
import { GetanModalProps } from "../types"
import { GetanButtonProps } from "@/components/Button"
import { GetanButtonGroupProps } from "@/components/ButtonGroup"

/**
 * 支持默认的取消，确认按钮
 */
export function useActions(props: Ref<GetanModalProps>, visible: Ref<boolean>, emits: (event: any, ...args: any[]) => void) {
    const cancelBtn: GetanButtonProps = {
        title: "取消",
        type: "default",
        onClick: () => {
            visible.value = false
            emits("on-cancel")
        }
    }

    const okBtn: GetanButtonProps = {
        title: "确定",
        type: "primary",
        onClick: () => {
            emits("on-ok", visible)
        }
    }

    /**
     * 默认按钮
     */
    const defaultActions = computed((): GetanButtonGroupProps => {
        const { cancel, ok } = unref(props)
        const btns: GetanButtonProps[] = []
        if (isObject(cancel)) {
            btns.push(cancel as GetanButtonProps)
        } else if (cancel !== false) {
            btns.push({ ...cancelBtn })
        }
        if (isObject(ok)) {
            btns.push(ok as GetanButtonProps)
        } else if (ok !== false) {
            btns.push({ ...okBtn })
        }
        return { items: btns, size: "small" }
    })

    return { defaultActions }
}
